package com.example.base.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.base.datafilter.DataFilter;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.security.domain.SysMenuEntity;
import com.example.base.security.domain.SysRoleEntity;
import com.example.base.security.domain.SysUserEntity;
import com.example.base.security.domain.vo.SysUserCommonVo;
import com.example.base.security.mapper.SysUserMapper;
import com.example.base.security.service.SysUserService;
import com.example.base.utils.Md5Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:51
 * @since JDK 1.8
 */
@Service("sysUserService")
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserEntity> implements SysUserService {


    @Override
    public  void commonValited(SysUserCommonVo t) throws GlobalExcetion {
        if(t.getUserId()!=null){
            SysUserEntity entity=getById(t.getUserId());
            if(entity==null){
                throw new GlobalExcetion(1,"用户不存在");
            }
            if(!t.getUsername().equals(entity.getUsername())){
                SysUserEntity sysUserEntity=selectUserByName(t.getUsername());
                if(sysUserEntity!=null){
                    throw new GlobalExcetion(1,"用户账号已被占用");
                }
            }
            if(t.getUserMail()!=null){
                if(!Objects.equals(t.getUserMail(),entity.getUserMail())){
                    SysUserEntity mail=selectUserByMail(t.getUserMail());
                    if(mail!=null){
                        throw new GlobalExcetion(1,"邮箱地址已被占用");
                    }
                }
            }
            if(!Objects.equals(t.getPhone(),entity.getPhone())){
                SysUserEntity phone=selectUserByPhone(t.getPhone());
                if(phone != null){
                    throw new GlobalExcetion(1,"手机号已被占用");
                }
            }
        }else {
            SysUserEntity sysUserEntity=selectUserByName(t.getUsername());
            if(sysUserEntity!=null){
                throw new GlobalExcetion(1,"用户账号已存在");
            }
            if(t.getUserMail()!=null){
                SysUserEntity mail=selectUserByMail(t.getUserMail());
                if(mail!=null){
                    throw new GlobalExcetion(1,"邮箱地址已被占用");
                }
            }
            SysUserEntity phone=selectUserByPhone(t.getPhone());
            if(phone != null){
                throw new GlobalExcetion(1,"手机号已被占用");
            }
        }
    }



    /**
     * 根据用户名查询实体
     * @Author Sans
     * @CreateTime 2019/9/14 16:30
     * @Param  username 用户名
     * @Return SysUserEntity 用户实体
     */
    @Override
    public SysUserEntity selectUserByName(String username) {
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserEntity::getUsername,username);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUserEntity selectUserByPhone(String phone) {
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserEntity::getPhone,phone);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUserEntity selectUserByMail(String mail) {
        QueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserEntity::getUserMail,mail);
        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
     * 通过用户ID查询角色集合
     * @Author Sans
     * @CreateTime 2019/9/18 18:01
     * @Param  userId 用户ID
     * @Return List<SysRoleEntity> 角色名集合
     */
    @Override
    public List<SysRoleEntity> selectSysRoleByUserId(Serializable userId) {
        return this.baseMapper.selectSysRoleByUserId(userId);
    }

    /**
     * 根据用户ID查询权限集合
     * @Author Sans
     * @CreateTime 2019/9/18 18:01
     * @Param userId 用户ID
     * @Return List<SysMenuEntity> 角色名集合
     */
    @Override
    public List<SysMenuEntity> selectSysMenuByUserId(Serializable userId) {
        return this.baseMapper.selectSysMenuByUserId(userId);
    }

    @DataFilter({"username","realname","userId","phone","roleId","userMail","sysRoleEntity","dept","deptId","createTime"})
    @Override
    public IPage<SysUserEntity> page(Integer pageNum, Integer pageSize, String name,List<Integer> deptId) {
        LambdaQueryWrapper<SysUserEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.or().like(SysUserEntity ::getUsername,name);
            lambdaQueryWrapper.or().like(SysUserEntity::getRealname,name);
            lambdaQueryWrapper.or().like(SysUserEntity::getPhone,name );
            lambdaQueryWrapper.or().like(SysUserEntity::getUserMail,name);
        }
        if(deptId!=null && deptId.size()>0){
            lambdaQueryWrapper.in(SysUserEntity::getDeptId,deptId);
        }
        lambdaQueryWrapper.orderByDesc(SysUserEntity ::getCreateTime);
        IPage<SysUserEntity> page=this.baseMapper.selectPage(new Page(pageNum,pageSize),lambdaQueryWrapper);
        return page;
    }


}
